<?php
/**
 * Created by PhpStorm.
 * User: mr_lee
 * Date: 2017/10/17
 * Time: 上午10:09
 */
namespace app\leader\controller;
use app\common\controller\AdminBase;
use app\common\model\Agent;
use app\common\model\CardSignup;
use app\common\model\Momerchant;
use app\common\model\OpenTemplate;
use app\common\model\System;
use app\common\model\WxchMenu;
use app\common\model\WxOpen;
use think\Controller;
use think\Request;
use think\Session;
use Wechat\Wxobjopen;

class Wxsystem extends AdminBase
{

    //自定义菜单
    public function wx_menu(){
        if(Request()->isPost()){
            $arr = request()->post();
            $arr['wid'] = 1;
            $res = $this->create_menu($arr);
            if($res['errcode'] == 0){
                $this->success('设置成功！','Wxsystem/wx_menu');
            }else{
                $this->error('设置失败！','Wxsystem/wx_menu');
            }
        }else{
            $WxchMenu = new WxchMenu();
            $map['wid']=1;
            $menu = $WxchMenu->where($map)->find();
            $this->assign('data',$menu);
            return $this->fetch();
        }
    }

    //生成自定义菜单
    public function create_menu($arr){
        $menu = new WxchMenu();
        $map['wid'] = $arr['wid'];
        $sj = $menu->where($map)->find();
        for ($i = 1; $i <= 5; $i++) {
            if ($sj['first1_' . $i . '_type'] == "view") {
                $s = 'url';
            } elseif ($sj['first1_' . $i . '_type'] == "click") {
                $s = 'key';
            }
            if (!empty($sj['first1_' . $i . ''])) {
                $arr1[] = array('type' => $sj['first1_' . $i . '_type'], 'name' => urlencode($sj['first1_' . $i . '']), $s => urlencode($sj['first1_' . $i . '_value']));
            }
        }
        for ($i = 1; $i <= 5; $i++) {
            if ($sj['second1_' . $i . '_type'] == "view") {
                $s = 'url';
            } elseif ($sj['second1_' . $i . '_type'] == "click") {
                $s = 'key';
            }
            if (!empty($sj['second1_' . $i . ''])) {
                $arr2[] = array('type' => $sj['second1_' . $i . '_type'], 'name' => urlencode($sj['second1_' . $i . '']), $s => urlencode($sj['second1_' . $i . '_value']));
            }
        }
        for ($i = 1; $i <= 5; $i++) {
            if ($sj['third1_' . $i . '_type'] == "view") {
                $s = 'url';
            } elseif ($sj['third1_' . $i . '_type'] == "click") {
                $s = 'key';
            }
            if (!empty($sj['third1_' . $i . ''])) {
                $arr3[] = array('type' => $sj['third1_' . $i . '_type'], 'name' => urlencode($sj['third1_' . $i . '']), $s => urlencode($sj['third1_' . $i . '_value']));
            }
        }
        if (!empty($sj['first'])) {
            if ($sj['first_type'] == "view") {
                $s = 'url';
            } elseif ($sj['first_type'] == "click") {
                $s = 'key';
            }
            if (empty($sj['first_value'])) {
                $sarr1 = array('name' => urlencode($sj['first']), 'sub_button' => $arr1);
            } else {
                $sarr1 = array('type' => $sj['first_type'], 'name' => urlencode($sj['first']), $s => urlencode($sj['first_value']));
            }
        } else {
            unset($sarr1);
        }
        if (!empty($sj['second'])) {
            if ($sj['second_type'] == "view") {
                $s = 'url';
            } elseif ($sj['second_type'] == "click") {
                $s = 'key';
            }
            if (empty($sj['second_value'])) {
                $sarr2 = array('name' => urlencode($sj['second']), 'sub_button' => $arr2);
            } else {
                $sarr2 = array('type' => $sj['second_type'], 'name' => urlencode($sj['second']), $s => urlencode($sj['second_value']));
            }
        } else {
            unset($sarr2);
        }
        if (!empty($sj['third'])) {
            if ($sj['third_type'] == "view") {
                $s = 'url';
            } elseif ($sj['third_type'] == "click") {
                $s = 'key';
            }
            if (empty($sj['third_value'])) {
                $sarr3 = array('name' => urlencode($sj['third']), 'sub_button' => $arr3);
            } else {
                $sarr3 = array('type' => $sj['third_type'], 'name' => urlencode($sj['third']), $s => urlencode($sj['third_value']));
            }
        } else {
            unset($sarr3);
        }
        $aarr = array($sarr1, $sarr2, $sarr3);
        $darr = array_filter($aarr);
        $arr = array('button' => $darr);
        $menu = urldecode(json_encode($arr, true));
        $res = $this->tj_menu($menu);
        return $res;
    }

    //提交菜单
    public function tj_menu($menu){
        $options = $this->options();
        $wxObj = new Wxobjopen($options);
        $s = $wxObj ->createMenu($menu);
        return $s;
    }

    //模版消息
    public function wx_tplmesg(){
        if (Request()->isPost()) {
            $res = $this->setindustry();
            if ($res['errcode'] == 0 && $res['errmsg'] == 'ok'){
                $this->success('设置成功！','wxsystem/wx_tplmesg');
            }else{
                $this->error('未开通模版消息！'.$res['errmsg'],'wxsystem/wx_tplmesg');
            }
        }else{
            $user = Session::get('user');
            var_dump($user);
            $Agent = new Agent();
            $uid = $user['uid'];
            $agents = $Agent->where('uid', $uid)->find();
            $this->assign('agents', $agents);
            return $this->fetch();
        }
    }

    //设置所属行业
    public function setindustry(){
        $industry_id1 = '1';
        $industry_id2 = '2';
        $option = $this->options();
        $wxObj = new Wxobjopen($option);
        $res = $wxObj->setTMIndustry($industry_id1,$industry_id2);
        if ($res['errcode'] == 0){
            $this->getTemlpId();
        }else{
            return $res;
        }
    }

    //获得模板ID
    public function getTemlpId(){
        $Momerchant = new Momerchant();
        $OpenTemplate = new OpenTemplate();
        //支付
        $tpl = [
            'OPENTM408727351',  //支付
            'OPENTM401833445',  //余额
            'OPENTM200772305'   //赠送
        ];
        $sarr = Session::get('user');
        $uid = $sarr['uid'];
        $wx_open_id = $Momerchant->where(['uid' => $uid])->value('wx_open_id');
        $option = $this->options();
        $wxObj = new Wxobjopen($option);
        for ($x=0; $x<3; $x++){
            $res = $wxObj->addTemplateMessage($tpl[$x]);
            if ($res['errcode'] == 0){
                $datas['type'] = 'pay_tpl';
                $datas['template_id'] = $res['template_id'];
                $datas['wx_open_id'] = $wx_open_id;
                $datas['addtime'] = time();
                $OpenTemplate->save($datas);
            }else{
                return $res;
            }
        }
    }

    //授权信息
    public function wx_authinfo(){
        $sarr = Session::get('user');
        $Agent=new Agent();
        $uid = $sarr['uid'];
        $user = $Agent->where('uid',$uid)->find();
        $wxopen=WxOpen::get($user['wx_open_id']);
        $this->assign('wxopen',$wxopen);
        $this->assign('user',$user);
        return $this->fetch();
    }

    //微信API参数  根据代理商当前登陆账号获取
    public function options(){
        Session::set('openid', null);
        $System=new System();
        $WxOpen=new WxOpen();
        $Agent=new Agent();
        $wx_open_id = '';
//        $config = $System->where(['id' => 1])->find();
        $users = $WxOpen->where(['id'=>$wx_open_id])->find();
        $config=$Agent->where(array('wx_open_id'=>$users['id']))->find();
        $options = array(
            'component_token' => $config['component_token'], // 填写你设定的key
            'component_encodingaeskey' => $config['component_encodingaeskey'], // 填写加密用的EncodingAESKey
            'component_appid' => $config['component_appid'], // 填写高级调用功能的app id
            'component_appsecret' => $config['component_appsecret'], // 填写高级调用功能的密钥
            'open_ticket' => $config['open_ticket'],
            'authorizer_appid' => $users['authorizer_appid'],
            'authorizer_refresh_token' => $users['authorizer_refresh_token'],
        );
        return $options;
    }

}